package com.fenghongzhang.day009;

import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.PointF;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import java.util.Random;

/**
 * 4种
 * 1,view
 * 2,viewgroup ->嵌套
 * 3,系统提供的 dialog  mydialog -> LinearLayout -> re fr
 * 4,组合
 */
public class MyView extends RelativeLayout {

    private Drawable[] drawables = new Drawable[5];
    private int width;
    private int height;
    //代码new
    public MyView(Context context) {
        super(context);
        init();
    }

    //xml
    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        drawables[0] = ContextCompat.getDrawable(getContext(),R.mipmap.ic_launcher);
        drawables[1] = ContextCompat.getDrawable(getContext(),R.mipmap.ic_launcher);
        drawables[2] = ContextCompat.getDrawable(getContext(),R.mipmap.ic_launcher);
        drawables[3] = ContextCompat.getDrawable(getContext(),R.mipmap.ic_launcher);
        drawables[4] = ContextCompat.getDrawable(getContext(),R.mipmap.ic_launcher);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width = getMeasuredWidth();
        height = getMeasuredHeight();
    }


    public void addImageView(){
        ImageView imageView = new ImageView(getContext());
        imageView.setImageDrawable(drawables[new Random().nextInt(drawables.length)]);
        addView(imageView);
        //给view设置一个动画
        bezierValueAnimator(imageView).start();

    }

    private ValueAnimator bezierValueAnimator(final ImageView view){
        MyEvaluator myEvaluator = new MyEvaluator(getPointF(), getPointF());

        PointF randomEndPoint = new PointF((float) (Math.random()*width), (float) (Math.random()*50));
        PointF randomStartPoint = new PointF((float) (width/2), (float) height);

        final ValueAnimator valueAnimator = ValueAnimator.ofObject(myEvaluator, randomStartPoint, randomEndPoint);

        valueAnimator.setDuration(3000);
        valueAnimator.setTarget(view);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                PointF pointF = (PointF) animation.getAnimatedValue();
                view.setX(pointF.x);
                view.setY(pointF.y);
// 这里顺便做一个alpha动画
//                view.setAlpha(1 - animation.getAnimatedFraction());
            }
        });

        return valueAnimator;
    }

    public PointF getPointF(){
        PointF pointF = new PointF();
        pointF.x = (float) (Math.random()*width);
        pointF.y = (float) (Math.random()*height/4);
        return pointF;
    }

    public void remove(){
        removeAllViews();
    }

    public class MyEvaluator implements TypeEvaluator<PointF>{

        private PointF pointF1;
        private PointF pointF2;


        public MyEvaluator(PointF pointF1, PointF pointF2) {
            this.pointF1 = pointF1;
            this.pointF2 = pointF2;
        }

        @Override
        public PointF evaluate(float time, PointF startValue, PointF endValue) {
            float timeLeft = 1.0f-time;
            PointF point = new PointF();//结果

            point.x = timeLeft * timeLeft * timeLeft * (startValue.x)
                    + 3 * timeLeft * timeLeft * time * (pointF1.x)
                    + 3 * timeLeft * time * time * (pointF2.x)
                    + time * time * time * (endValue.x);

//            Log.i(TAG, "evaluate: "+point.x);
            point.y = timeLeft * timeLeft * timeLeft * (startValue.y)
                    + 3 * timeLeft * timeLeft * time * (pointF1.y)
                    + 3 * timeLeft * time * time * (pointF2.y)
                    + time * time * time * (endValue.y);

//            Log.i(TAG, "evaluate: "+point.y);
            return point;
        }
    }


}
